package com.esun.rabbit2.viewer.mainact;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import org.androidpn.client.Constants;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.esun.config.Config;
import com.esun.config.DistributionChannels;
import com.esun.config.Key;
import com.esun.config.Language;
import com.esun.model.LoadingInfo;
import com.esun.model.UserInfo;
import com.esun.network.ConnectionChangeReceiver;
import com.esun.rabbit2.communicaters.StopLotteryListCommunicater;
import com.esun.rabbit2.communicaters.lotteryhalltopmsg.GetActivityInfoCommunicater;
import com.esun.rabbit2.viewer.ActivityWebViewActivity;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.BeiDanMainThread;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.BeidanAgp;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.DaletouAgp;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.MoreLotteryWebView;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.ShengFuCaiMainThread;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.ShiyixuanwuMainThread;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.ShuangseqiuAgp;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.SportteryBasketballAgp;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.SportteryFootballAgp;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.SportteryFootballShengfuAgp;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.SportteryMainThread;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.SsqAndDltMainThread;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.daletouagp.DaletouChooseAct;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.shiyixuanwu.ShiyixuanwuActGroup;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.shuangseqiuagp.ShuangseqiuChooseAct;
import com.esun.ui.R;
import com.esun.util.StringUtil;
import com.esun.util.ViewerUtil;
import com.esun.util.log.EDebug;
import com.esun.util.magic.magic;

/**
 * @Title: LotteryHallAct.java
 * @Package com.esun.ui
 * @Description: (购彩大厅页面Activity)
 * @author City jiangsy@500wan.com
 * @date 2011-5-13 下午05:23:29 copy right 深圳市易讯天空网络技术有限公司
 */
public class LotteryHallAct extends Activity implements OnClickListener {

	private Intent intentToshuangseqiu;
	private Intent intentToDaletou;

	private ImageView imgShuangseqiu; // 双色球
	private ImageView imgDaletou; // 大乐透
	private ImageView imgShiyixuanwu; // 十一选五
	private ImageView imgSportteryFootball; // 竞彩足球
	private ImageView imgSportteryBasketball; // 竞彩篮球
	private ImageView imgShengFuCai;// 足球胜负彩
	private ImageView imgBeiDan;// 北京单场

	private ImageButton btnRefresh;
	private ProgressBar progressBar;

	private Intent intentToSportteryFootball;
	private Bundle bundleToSportteryFootbal;

	private Intent intentToSportteryBasketball;
	private Bundle bundleToSportteryBasketball;

	private ShiyixuanwuMainThread shiyixuanwuMainThread; // 主界面11选5刷新线程
	private SsqAndDltMainThread shuangseqiuMainThread; // 主界面双色球刷新线程
	private SsqAndDltMainThread daletouMainThread; // 主界面大乐透刷新线程
	private SportteryMainThread footballMainThread; // 主界面竞彩足球刷新线程
	private SportteryMainThread basketballMainThread; // 主界面竞彩篮球刷新线程
	private BeiDanMainThread beiDanMainThread;// 主界面北单刷新线程
	private ShengFuCaiMainThread shengFuCaiMainThread;// 胜负彩刷新线程

	private RelativeLayout relativeLayoutTop; // 广告图片
	private LinearLayout linearLayoutTop; // 广告提示语
	private Button btnImgCancel;
	private TextView tvTopMsg;
	private ImageView imgForAdd;
	private ViewFlipper imgViewFlipper;

	/** add by billy 20120308 更多彩种 */
	private Button btnMoreLottery;
	// 11选5消息显示
	private RelativeLayout rlShiyixuanwuLeftTime;
	private TextView tvShiYiXuanWuMsg;
	// 竞彩足球消息显示
	private LinearLayout llFootballInfo;
	private TextView tvFootballMsg;
	// 竞彩篮球消息显示
	private LinearLayout llBasketballInfo;
	private TextView tvBasketballMsg;

	private LinearLayout llItemSsqToFill;
	private LinearLayout llItemDltToFill;

	private LoadingInfo loadingInfo = LoadingInfo.getInstance();
	/**
	 * @Fields imageList : 活动图片列表
	 */
	private ArrayList<ImageView> imageList;

	/** 彩种是否停售 */
	private boolean isStopShuangSeQiu = false;
	private boolean isStopDaLeTou = false;
	private boolean isStopShiYiXuanWu = false;
	private boolean isStopFootball = false;
	private boolean isStopBasketball = false;
	private boolean isStopBeiDan = false;
	private boolean isStopShengFuCai = false;

	private boolean isGettingImage = false;

	/** 获取彩种是否停售接口 */
	private StopLotteryListCommunicater stopLotteryListCommunicater;
	/** 获取活动图片以及链接通信接口 */
	private GetActivityInfoCommunicater activityCommunicater;
	private Handler handler;

	/** 获取停售列表成功 */
	private final int GET_STOP_LOTTERY_LIST_SUCCEED = 1001;
	/** 获取停售列表失败 */
	private final int GET_STOP_LOTTERY_LIST_FAILED = 1002;
	/** 获取活动图片成功 */
	private final int GET_ACTIVITY_IMAGE_SUCCEED = 1003;
	/** 获取活动图片失败 */
	private final int GET_ACTIVITY_IMAGE_FAILED = 1004;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lotteryhall_act);
		initHandler();

		// 初始化界面
		inIt();
		// 判断是否有彩种停售
		isLotteryStop();
		// 初始化通信
		refresh();
	}

	private void getActivityImageThread() {
		EDebug.o(this, "获取图片线程开始is getting image = " + isGettingImage);
		if (!isGettingImage) {
			new Thread() {
				@Override
				public void run() {
					EDebug.o(this, "开始获取活动图片信息");
					isGettingImage = true;
					EDebug.o(this, "获取图片线程is getting image = " + isGettingImage);
					super.run();
					if (activityCommunicater == null) {
						activityCommunicater = new GetActivityInfoCommunicater();
					}
					try {
						String channel = DistributionChannels.QUDAO;
						String version = UserInfo.getUserInfo().getCurrentVersion();
						boolean isSucceed = activityCommunicater.isGetInfoSucced(channel, version);
						if (isSucceed) {
							EDebug.o(this, "获取活动图片信息成功");
							imageList.clear();
							String flag = activityCommunicater.getResult().get(Key.KEY_ISACTIVITYVALID);
							String imgURL = activityCommunicater.getResult().get(Key.KEY_ACTIVITYIMGURL);
							String jumpURL = activityCommunicater.getResult().get(Key.KEY_ACTIVITYWEBVIEW);
							EDebug.o("isActivity = " + flag + " 图片地址 = " + imgURL + " 图片跳转地址 = " + jumpURL);
							if (flag != null && flag.equals(Config.NUM1)) {
								if (imgURL != null && !imgURL.equals(Config.NULLMARK)) {
									// 拆分图片以及链接列表
									String imageURL[] = imgURL.split("\\$");
									if (jumpURL == null) {
										// 如果跳转链接为空
										for (int i = 0; i < imageURL.length; i++) {
											// 增加一个图片以及空链接
											addActivityImages(imageURL[i], Config.NULLMARK);
										}
									} else {
										String imgJumpURL[] = jumpURL.split("\\$");

										for (int i = 0; i < imageURL.length; i++) {
											// 增加一个图片以及对应链接
											if (i >= imgJumpURL.length) {
												addActivityImages(imageURL[i], Config.NULLMARK);
											} else {
												addActivityImages(imageURL[i], imgJumpURL[i]);
											}
										}
									}
								}
							}

							Message msg = handler.obtainMessage(GET_ACTIVITY_IMAGE_SUCCEED, null);
							handler.sendMessage(msg);
						} else {
							EDebug.o(this, "获取活动图片信息失败");
							Message msg = handler.obtainMessage(GET_ACTIVITY_IMAGE_FAILED, null);
							handler.sendMessage(msg);
						}
					} catch (Exception e) {
						EDebug.o(this, "获取活动图片信息异常");
						Message msg = handler.obtainMessage(Config.CONNECTION_FAIL, null);
						handler.sendMessage(msg);
					}
					isGettingImage = false;
					EDebug.o(this, "获取图片线程结束is getting image = " + isGettingImage);
				}
			}.start();
		}
	}

	/**
	 * @Title: topImgShow
	 * @Description: 顶部广告图片是否显示
	 * @param: 设定文件
	 * @return: void 返回类型
	 * @throws
	 */
	private void topImgShow() {

		imgViewFlipper.removeAllViews();

		for (int i = 0; i < imageList.size(); i++) {
			if (imageList.get(i) != null) {
				imgViewFlipper.addView(imageList.get(i), LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
			}
		}
		// 如果活动大于1个，开始自动切换界面
		if (imgViewFlipper.getChildCount() > 1) {

			// 设置View进入屏幕时候使用的动画
			imgViewFlipper.setInAnimation(inFromRightAnimation());
			// 设置View退出屏幕时候使用的动画
			imgViewFlipper.setOutAnimation(outToLeftAnimation());
			// 开始循环播放图片
			imgViewFlipper.startFlipping();
			// 显示广告控件
			imgViewFlipper.setVisibility(View.VISIBLE);
		} else if (imgViewFlipper.getChildCount() < 1 && imgViewFlipper.getChildCount() > 0) {
			// 显示广告控件
			imgViewFlipper.setVisibility(View.VISIBLE);
		}
	}

	/**
	 * @throws Exception
	 * @Title: addActivityImages图片以及对应
	 * @Description: 在活动图片增加一个链接
	 * @param: @param imgURL 图片下载地址
	 * @param: @param imgJumpURL 点击图片跳转webview链接
	 * @return: void 返回类型
	 * @throws
	 */
	private void addActivityImages(String imgURL, final String imgJumpURL) throws Exception {
		InputStream inputStream;
		inputStream = getImageStream(imgURL);
		Bitmap bmp = BitmapFactory.decodeStream(inputStream);
		ImageView image = new ImageView(LotteryHallAct.this);
		image.setImageBitmap(bmp);
		image.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (imgJumpURL != null && !imgJumpURL.equals(Config.NULLMARK)) {
					String ck = UserInfo.getUserInfo().getUserSession();
					String userName = UserInfo.getUserInfo().getUserName();
					String userPwd = UserInfo.getUserInfo().getUserPwd();
					String channel = DistributionChannels.QUDAO;
					String platform = Config.ANDROID;
					String pushIMEI = getSharedPreferences(Config.APP_PACKAGE_INFO, Context.MODE_PRIVATE).getString(Constants.DEVICE_ID, Config.DEFAULT_LEAGUAGE_NUM);
					String version;
					try {
						version = LotteryHallAct.this.getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
					} catch (NameNotFoundException e) {
						version = Config.NUM0;
					}
					String url = imgJumpURL + "?ck=" + ck + "&username=" + userName + "&userpwd=" + userPwd + "&channel=" + channel + "&platform=" + platform + "&version=" + version + "&userimei="
							+ pushIMEI;
					EDebug.o(this, "activity url = " + url);
					startActivity(new Intent(LotteryHallAct.this, ActivityWebViewActivity.class).putExtra(Key.KEY_URL, url));
				}
			}
		});
		EDebug.o(this, "image list size = " + imageList.size());
		imageList.add(image);
	}

	/**
	 * @Title: isLotteryStop
	 * @Description: 判断彩种是否停售
	 * @param: 设定文件
	 * @return: void 返回类型
	 * @throws
	 */
	private void isLotteryStop() {

		String isStop = loadingInfo.getIsstop();
		if (isStop != null && !isStop.equals(Config.NULLMARK)) {
			if (loadingInfo.getIsstop().equals(Config.NUM1)) {
				// 所有彩种停售
				setAllState(true);
				// 显示停售提示信息
				linearLayoutTop = (LinearLayout) findViewById(R.id.lotteryhallact_linear_msgTop);
				linearLayoutTop.setVisibility(View.VISIBLE);
				String stopMsg = loadingInfo.getStopnotes();
				tvTopMsg = (TextView) findViewById(R.id.lotteryhallact_tv_msgTop);
				if (stopMsg != null && !stopMsg.equals(Config.NULLMARK)) {
					tvTopMsg.setText(stopMsg);
				} else {
					tvTopMsg.setText(Language.MSG_STOP_LOTTERY);
				}
			} else {
				// 不是所有彩种停售
				String stopLottery = loadingInfo.getLotid();
				// 判断是哪些彩种停售
				if (stopLottery != null && !stopLottery.equals(Config.NULLMARK)) {
					String[] lotteryList = stopLottery.split("\\,");

					if (StringUtil.isContain(lotteryList, Config.SHUANGSEQIU)) {
						isStopShuangSeQiu = true;
					}
					if (StringUtil.isContain(lotteryList, Config.DALETOU)) {
						isStopDaLeTou = true;
					}
					if (StringUtil.isContain(lotteryList, Config.SHIYIXUANWU)) {
						isStopShiYiXuanWu = true;
					}
					if (StringUtil.isContain(lotteryList, Config.JINGCAIZUQIU)) {
						isStopFootball = true;
					}
					if (StringUtil.isContain(lotteryList, Config.JINGCAILANQIU)) {
						isStopBasketball = true;
					}
					if (StringUtil.isContain(lotteryList, Config.BEIJINGZUQIUDANCHANG)) {
						isStopBeiDan = true;
					}
					if (StringUtil.isContain(lotteryList, Config.SHENGFUCAI)) {
						isStopShengFuCai = true;
					}
				}
			}
		}
	}

	/*
	 * 初始化
	 */
	private void inIt() {

		imageList = new ArrayList<ImageView>();

		btnRefresh = (ImageButton) this.findViewById(R.id.lotteryhall_act_btn_refresh);
		btnRefresh.setOnClickListener(this);
		progressBar = (ProgressBar) this.findViewById(R.id.lotteryinfoact_pbr_refresh);
		progressBar.setVisibility(View.INVISIBLE);

		intentToshuangseqiu = new Intent();
		intentToshuangseqiu.setClass(LotteryHallAct.this, ShuangseqiuAgp.class);

		intentToDaletou = new Intent();
		intentToDaletou.setClass(LotteryHallAct.this, DaletouAgp.class);

		// 进入3GWAP网页购彩
		btnMoreLottery = (Button) findViewById(R.id.lotteryhallact_btn_more);
		btnMoreLottery.setOnClickListener(this);

		ProgressBar pbShiyixuanwu = (ProgressBar) this.findViewById(R.id.lotteryhall_act_pb_shiyixuanwu);
		rlShiyixuanwuLeftTime = (RelativeLayout) this.findViewById(R.id.lottery_hall_rl_11x5_jiezhi_time);
		tvShiYiXuanWuMsg = (TextView) this.findViewById(R.id.lotteryhall_act_tv_shiyixuanwuMsg);
		int color = this.getResources().getColor(R.color.gcdt_txt_term);
		// 十一选五通信线程
		shiyixuanwuMainThread = new ShiyixuanwuMainThread(rlShiyixuanwuLeftTime, pbShiyixuanwu, color);

		// 双色球奖池及开奖信息Layout
		llItemSsqToFill = (LinearLayout) findViewById(R.id.lottery_hall_item_shuangsqiu_to_fill);
		// 双色球通信线程
		shuangseqiuMainThread = new SsqAndDltMainThread(llItemSsqToFill, Config.SHUANGSEQIU, LotteryHallAct.this);

		// 大乐透奖池及开奖信息Layout
		llItemDltToFill = (LinearLayout) findViewById(R.id.lottery_hall_item_daletou_to_fill);
		daletouMainThread = new SsqAndDltMainThread(llItemDltToFill, Config.DALETOU, LotteryHallAct.this);

		// 竞彩足球队伍信息Layout
		llFootballInfo = (LinearLayout) findViewById(R.id.lotteryhallact_ll_toSportteryFootball_team);
		ProgressBar progressBar = (ProgressBar) findViewById(R.id.lotteryhall_act_pb_football);
		tvFootballMsg = (TextView) findViewById(R.id.lotteryhall_act_tv_football_msg);
		// 竞彩足球刷新线程
		footballMainThread = new SportteryMainThread(llFootballInfo, progressBar, Config.JINGCAIZUQIU);

		// 竞彩篮球队伍信息Layout
		llBasketballInfo = (LinearLayout) findViewById(R.id.lotteryhallact_ll_toSportteryBsaketball_team);
		ProgressBar basketballProgressBar = (ProgressBar) findViewById(R.id.lotteryhall_act_pb_basketball);
		tvBasketballMsg = (TextView) findViewById(R.id.lotteryhall_act_tv_basketball_msg);
		// 竞彩篮球刷新线程
		basketballMainThread = new SportteryMainThread(llBasketballInfo, basketballProgressBar, Config.JINGCAILANQIU);

		// 双色球点击事件，购买
		imgShuangseqiu = (ImageView) this.findViewById(R.id.lottery_hall_item_shuangsqiu);
		imgShuangseqiu.setOnClickListener(this);

		// 大乐透点击事件，购买
		imgDaletou = (ImageView) this.findViewById(R.id.lottery_hall_item_daletou);
		imgDaletou.setOnClickListener(this);

		// 十一选五点击事件，购买
		imgShiyixuanwu = (ImageView) this.findViewById(R.id.lottery_hall_item_shiyixuanwu);
		imgShiyixuanwu.setOnClickListener(this);

		// 竞彩足球点击事件，购买
		imgSportteryFootball = (ImageView) this.findViewById(R.id.lottery_hall_item_football);
		imgSportteryFootball.setOnClickListener(this);

		// 竞彩篮球点击事件，购买
		imgSportteryBasketball = (ImageView) this.findViewById(R.id.lottery_hall_item_basketball);
		imgSportteryBasketball.setOnClickListener(this);

		/** 2011-08-12 city add for sporttery football */
		// 竞彩足球选号
		intentToSportteryFootball = new Intent();
		intentToSportteryFootball.setClass(LotteryHallAct.this, SportteryFootballAgp.class);

		// 竞彩篮球相关控件
		intentToSportteryBasketball = new Intent();
		intentToSportteryBasketball.setClass(LotteryHallAct.this, SportteryBasketballAgp.class);
		// 竞彩篮球选号
		intentToSportteryBasketball = new Intent();
		intentToSportteryBasketball.setClass(LotteryHallAct.this, SportteryBasketballAgp.class);

		// 胜负彩
		imgShengFuCai = (ImageView) findViewById(R.id.lottery_hall_item_shengfucai);
		imgShengFuCai.setOnClickListener(this);
		LinearLayout shengFuCaiItem = (LinearLayout) findViewById(R.id.lotteryhallact_linear_shengfucai);
		shengFuCaiMainThread = new ShengFuCaiMainThread(shengFuCaiItem, LotteryHallAct.this);

		// 北京单场
		imgBeiDan = (ImageView) findViewById(R.id.lottery_hall_item_beidan);
		imgBeiDan.setOnClickListener(this);
		LinearLayout beidanItem = (LinearLayout) findViewById(R.id.lotteryhallact_linear_beidan);
		beiDanMainThread = new BeiDanMainThread(beidanItem);

		/** 2012-01-13 city add for 1.4.1 happy new year */
		relativeLayoutTop = (RelativeLayout) findViewById(R.id.lotteryhallact_relative_imgTop);
		btnImgCancel = (Button) findViewById(R.id.lotteryhallact_btn_cancelImg);
		btnImgCancel.setOnClickListener(this);
		imgForAdd = (ImageView) findViewById(R.id.lotteryhallact_img_forAdd);
		imgForAdd.setOnClickListener(this);
		imgViewFlipper = (ViewFlipper) findViewById(R.id.lotteryhallact_viewflipper_activity);

	}

	/*
	 * 点击事件
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		// 双色球
		case R.id.lottery_hall_item_shuangsqiu:

			if (!isStopShuangSeQiu) {
				if (shuangseqiuMainThread.isCanbuy()) {
					resetShowType();
					intentToshuangseqiu.putExtra(Config.IS_REBUY, false);
					EDebug.o(this, "click to shuangseqiu choose");
					startActivity(intentToshuangseqiu);
				} else {
					ViewerUtil.showTipDialog(this, Language.POPLOTTERYTIP_IS_LOADING, false);
				}
			} else {
				ViewerUtil.showTipDialog(this, Language.POPLOTTERYTIP_LOTTERY_STOP, false);
			}
			break;

		// 大乐透
		case R.id.lottery_hall_item_daletou:

			if (!isStopDaLeTou) {
				if (daletouMainThread.isCanbuy()) {
					resetShowType();
					EDebug.o(this, "click to daletou choose");
					intentToDaletou.putExtra(Config.CURRENT_TAB_NUM, 0).putExtra(Config.IS_REBUY, false);
					startActivity(intentToDaletou);
				} else {
					ViewerUtil.showTipDialog(this, Language.POPLOTTERYTIP_IS_LOADING, false);
				}
			} else {
				ViewerUtil.showTipDialog(this, Language.POPLOTTERYTIP_LOTTERY_STOP, false);
			}
			break;

		// 竞彩足球
		case R.id.lottery_hall_item_football:
			if (!isStopFootball) {
				bundleToSportteryFootbal = new Bundle();
				bundleToSportteryFootbal.putInt("CURRENT_TAB_NUM", 0);
				intentToSportteryFootball.putExtras(bundleToSportteryFootbal);
				EDebug.o(this, "click to shuangseqiu choose");
				startActivity(intentToSportteryFootball);
			} else {
				ViewerUtil.showTipDialog(this, Language.POPLOTTERYTIP_LOTTERY_STOP, false);
			}
			break;

		// 竞彩篮球
		case R.id.lottery_hall_item_basketball:
			if (!isStopBasketball) {
				bundleToSportteryBasketball = new Bundle();
				bundleToSportteryBasketball.putInt("CURRENT_TAB_NUM", 0);
				intentToSportteryBasketball.putExtras(bundleToSportteryBasketball);
				EDebug.o(this, "click to SportteryBasketball choose");
				startActivity(intentToSportteryBasketball);
			} else {
				ViewerUtil.showTipDialog(this, Language.POPLOTTERYTIP_LOTTERY_STOP, false);
			}
			break;

		// 十一选五
		case R.id.lottery_hall_item_shiyixuanwu:
			if (!isStopShiYiXuanWu) {
				if (shiyixuanwuMainThread.isCanbuy()) {
					startActivity(new Intent().setClass(this, ShiyixuanwuActGroup.class).putExtra(Config.IS_REBUY, false));
				} else {
					ViewerUtil.showTipDialog(this, Language.POPLOTTERYTIP_IS_LOADING, false);
				}
			} else {
				ViewerUtil.showTipDialog(this, Language.POPLOTTERYTIP_LOTTERY_STOP, false);
			}
			break;

		// 胜负彩
		case R.id.lottery_hall_item_shengfucai:

			Intent intentSF = new Intent();
			intentSF.setClass(LotteryHallAct.this, SportteryFootballShengfuAgp.class);
			startActivity(intentSF.putExtra("CURRENT_TAB_NUM", 0));

			break;

		// 北单
		case R.id.lottery_hall_item_beidan:

			Intent intentBD = new Intent();
			intentBD.setClass(LotteryHallAct.this, BeidanAgp.class);
			startActivity(intentBD.putExtra("Current_Tab_Num", 0));

			break;

		// 刷新按钮
		case R.id.lotteryhall_act_btn_refresh:

			refresh();

			break;

		// 关闭广告图片
		case R.id.lotteryhallact_btn_cancelImg:
			relativeLayoutTop.setVisibility(View.GONE);
			break;

		// 点击广告图片链接web
		case R.id.lotteryhallact_img_forAdd:
			Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(Config.URL_ADVERTISING));
			startActivity(intent);
			break;

		// 更多彩种按钮
		case R.id.lotteryhallact_btn_more:

			startActivity(new Intent(LotteryHallAct.this, MoreLotteryWebView.class));

			break;

		default:
			break;
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Activity parent = getParent();
			if (parent != null) {
				parent.onKeyDown(keyCode, event);
				return true;
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	private void resetShowType() {
		// ShuangseqiuHemaiAct.showType = 0;
		ShuangseqiuChooseAct.showType = 0;
		DaletouChooseAct.showType = 0;

	}

	/**
	 * @Title: getStopListThread
	 * @Description: 获取停售彩种列表
	 * @param: 设定文件
	 * @return: void 返回类型
	 * @throws
	 */
	@SuppressWarnings("unused")
	private void getStopListThread() {
		new Thread() {
			@Override
			public void run() {
				super.run();

				if (stopLotteryListCommunicater == null) {
					stopLotteryListCommunicater = new StopLotteryListCommunicater();
				}

				try {
					boolean isSucceed = stopLotteryListCommunicater.getStopLotteryList();
					Message msg;
					if (isSucceed) {
						String strList = stopLotteryListCommunicater.getResult().get(Key.KEY_LOTID);
						msg = handler.obtainMessage(GET_STOP_LOTTERY_LIST_SUCCEED, strList);
					} else {
						msg = handler.obtainMessage(GET_STOP_LOTTERY_LIST_FAILED, null);
					}
					handler.sendMessage(msg);
				} catch (Exception e) {
					Message msg = handler.obtainMessage(Config.CONNECTION_FAIL, null);
					handler.sendMessage(msg);
				}

			}
		}.start();
	}

	private void initHandler() {
		handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);

				switch (msg.what) {

				case GET_STOP_LOTTERY_LIST_SUCCEED:

					String strList = (String) msg.obj;
					// 判断是哪些彩种停售
					if (strList != null && !strList.equals(Config.NULLMARK)) {
						String[] lotteryList = strList.split("\\,");
						EDebug.o(this, "print by billy =>>************1***********");
						if (StringUtil.isContain(lotteryList, Config.SHUANGSEQIU)) {
							isStopShuangSeQiu = true;
							ssqStopLottery();
						} else {
							isStopShuangSeQiu = false;
						}
						if (StringUtil.isContain(lotteryList, Config.DALETOU)) {
							isStopDaLeTou = true;
							dltStopLottery();
						} else {
							isStopDaLeTou = false;
						}
						if (StringUtil.isContain(lotteryList, Config.SHIYIXUANWU)) {
							isStopShiYiXuanWu = true;
							syxwStopLottery();
						} else {
							isStopShiYiXuanWu = false;
						}
						if (StringUtil.isContain(lotteryList, Config.JINGCAIZUQIU)) {
							footballStopLottery();
							isStopFootball = true;
						} else {
							isStopFootball = false;
						}
						if (StringUtil.isContain(lotteryList, Config.JINGCAILANQIU)) {
							basketballStopLottery();
							isStopBasketball = true;
						} else {
							isStopBasketball = false;
						}
						if (StringUtil.isContain(lotteryList, Config.BEIJINGZUQIUDANCHANG)) {
							beiDanMainThread.showStopView();
							isStopBeiDan = true;
						} else {
							isStopBeiDan = false;
						}
						if (StringUtil.isContain(lotteryList, Config.SHENGFUCAI)) {
							shengFuCaiMainThread.showStopLotteryView();
							isStopShengFuCai = true;
						} else {
							isStopShengFuCai = false;
						}
					} else {
						// 为空则全部可以售彩
						setAllState(false);
					}
					// 刷新
					refresh();

					break;
				case GET_STOP_LOTTERY_LIST_FAILED:

					Toast.makeText(LotteryHallAct.this, stopLotteryListCommunicater.getServerMsg(), Toast.LENGTH_SHORT);

					break;

				case GET_ACTIVITY_IMAGE_SUCCEED:

					topImgShow();

					break;

				case GET_ACTIVITY_IMAGE_FAILED:

					break;

				case Config.CONNECTION_FAIL:

					Toast.makeText(LotteryHallAct.this, Language.STR_CONNECTIONFAIL, Toast.LENGTH_SHORT).show();

					break;

				default:

					break;

				}
			}
		};
	}

	/**
	 * @Title: refresh
	 * @Description: 刷新购彩大厅界面期号以及开奖时间
	 * @param: 设定文件
	 * @return: void 返回类型
	 * @throws
	 */
	private void refresh() {
		// 获取活动信息
		getActivityImageThread();

		// 判断11选5是否需要获取期号等信息
		if (!isStopShiYiXuanWu) {
			shiyixuanwuMainThread.refresh();
			tvShiYiXuanWuMsg.setText(Language.MSG_SYXW_FANJIANGLV);
		} else {
			syxwStopLottery();
		}
		// 判断双色球是否需要获取期号等信息
		if (!isStopShuangSeQiu) {
			shuangseqiuMainThread.refresh();
		} else {
			ssqStopLottery();
		}
		// 判断大乐透是否需要获取期号等信息
		if (!isStopDaLeTou) {
			daletouMainThread.refresh();
		} else {
			dltStopLottery();
		}
		// 判断足球是否需要获取期号等信息
		if (!isStopFootball) {
			footballMainThread.refresh();
			tvFootballMsg.setText(Language.MSG_FOOTBALL_FANJIANGLV);
		} else {
			footballStopLottery();
		}
		// 判断篮球是否需要获取期号等信息
		if (!isStopBasketball) {
			basketballMainThread.refresh();
			tvBasketballMsg.setText(Language.MSG_BASKETBALL_FANJIANGLV);
		} else {
			basketballStopLottery();
		}
		// 判断胜负彩是否需要获取期号等信息
		if (!isStopShengFuCai) {
			shengFuCaiMainThread.refresh();
		} else {
			shengFuCaiMainThread.showStopLotteryView();
		}
		// 判断北单是否需要获取期号等信息
		if (!isStopBeiDan) {
			beiDanMainThread.refresh();
		} else {
			beiDanMainThread.showStopView();
		}
	}

	/**
	 * @Title: setAllState
	 * @Description: 统一设置所有彩种开关状态
	 * @param: @param b false为可以购买 true 为暂停售彩
	 * @return: void 返回类型
	 * @throws
	 */
	private void setAllState(boolean state) {
		isStopShuangSeQiu = state;
		isStopDaLeTou = state;
		isStopShiYiXuanWu = state;
		isStopFootball = state;
		isStopBasketball = state;
	}

	/**
	 * @Title: syxwStopLottery
	 * @Description: 11选5停售显示方法
	 * @param: 设定文件
	 * @return: void 返回类型
	 * @throws
	 */
	private void syxwStopLottery() {
		tvShiYiXuanWuMsg.setText(Language.POPLOTTERYTIP_LOTTERY_STOP);
		shiyixuanwuMainThread.leftTimeGone();
		shiyixuanwuMainThread.progressGone();
	}

	/**
	 * @Title: dltStopLottery
	 * @Description: 大乐透停售显示方法
	 * @param: 设定文件
	 * @return: void 返回类型
	 * @throws
	 */
	private void dltStopLottery() {
		daletouMainThread.showInfoStopLottery();
	}

	/**
	 * @Title: ssqStopLottery
	 * @Description: 双色球停售显示方法
	 * @param: 设定文件
	 * @return: void 返回类型
	 * @throws
	 */
	private void ssqStopLottery() {
		shuangseqiuMainThread.showInfoStopLottery();
	}

	/**
	 * @Title: footballStopLottery
	 * @Description: 竞彩足球停售显示方法
	 * @param: 设定文件
	 * @return: void 返回类型
	 * @throws
	 */
	private void footballStopLottery() {
		tvFootballMsg.setText(Language.POPLOTTERYTIP_LOTTERY_STOP);
		llFootballInfo.setVisibility(View.GONE);
		footballMainThread.progressBarGone();
	}

	/**
	 * @Title: basketballStopLottery
	 * @Description: 竞彩篮球
	 * @param: 设定文件
	 * @return: void 返回类型
	 * @throws
	 */
	private void basketballStopLottery() {
		tvBasketballMsg.setText(Language.POPLOTTERYTIP_LOTTERY_STOP);
		llBasketballInfo.setVisibility(View.GONE);
		basketballMainThread.progressBarGone();
	}

	@Override
	protected void onPause() {

		super.onPause();
		this.unregisterReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver());
	}

	@Override
	protected void onResume() {

		super.onResume();
		this.registerReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver(), ConnectionChangeReceiver.getIntentFilter());
	}

	// 获取图片
	public InputStream getImageStream(String path) throws Exception {
		URL url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setConnectTimeout(5 * 1000);
		conn.setRequestMethod("GET");
		if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
			return conn.getInputStream();
		} else {
			return null;
		}
	}

	/**
	 * 定义从右侧进入的动画效果
	 * 
	 * @return
	 */
	protected Animation inFromRightAnimation() {
		Animation inFromRight = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
		inFromRight.setDuration(500);
		inFromRight.setInterpolator(new AccelerateInterpolator());
		return inFromRight;
	}

	/**
	 * 定义从左侧退出的动画效果
	 * 
	 * @return
	 */
	protected Animation outToLeftAnimation() {
		Animation outtoLeft = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
		outtoLeft.setDuration(500);
		outtoLeft.setInterpolator(new AccelerateInterpolator());
		return outtoLeft;
	}

	/**
	 * 定义从左侧进入的动画效果
	 * 
	 * @return
	 */
	protected Animation inFromLeftAnimation() {
		Animation inFromLeft = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
		inFromLeft.setDuration(500);
		inFromLeft.setInterpolator(new AccelerateInterpolator());
		return inFromLeft;
	}

	/**
	 * 定义从右侧退出时的动画效果
	 * 
	 * @return
	 */
	protected Animation outToRightAnimation() {
		Animation outtoRight = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
		outtoRight.setDuration(500);
		outtoRight.setInterpolator(new AccelerateInterpolator());
		return outtoRight;
	}

}
